﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FB.DataAccess.DataInf;
using FB.Utilities;

namespace FB.DataAccess
{
    public class ChanelCategoryDAO : IChanelCategoryDAO
    {
        private static FBDATA dataContext;

        private IRepository<ChanelCategory> repository;
        public ChanelCategoryDAO()
        {
            dataContext = new FBDATA();
            repository = new RepositoryDAO<ChanelCategory>(dataContext);
        }

        public IEnumerable<ChanelCategory> GetAllChanelCategory()
        {
            return repository.GetQuery().OrderBy(aobj => aobj.Name);
        }
        public ChanelCategory GetChanelCategoryByID(Guid id)
        {
            return repository.Find(obj => obj.ID == id).SingleOrDefault();
        }
        public ChanelCategory GetLastRecord()
        {
            return dataContext.ChanelCategories.OrderByDescending(obj => obj.ID).FirstOrDefault();
        }

        public bool UpdateChanelCategory(ChanelCategory inChanelCategory)
        {
            if (inChanelCategory == null) return false;
            ChanelCategory upChanelCategory = repository.First(sobj => sobj.ID == inChanelCategory.ID);
            if (upChanelCategory == null) return false;
            upChanelCategory.Name = inChanelCategory.Name;
            upChanelCategory.Description = inChanelCategory.Description;
            upChanelCategory.Enable = inChanelCategory.Enable;
            return repository.SaveChanges() > 0;
        }
        public bool InsertChanelCategory(ChanelCategory inChanelCategory)
        {
            if (inChanelCategory == null) return false;
            repository.Add(inChanelCategory);
            return repository.SaveChanges() > 0;
        }

        public ReturnValue<bool> DelChanelCategory(Guid id)
        {
            ReturnValue<bool> ret = new ReturnValue<bool>(false, "");
            try
            {
                ChanelCategory objDel = repository.First(obj => obj.ID == id);
                ret = DelChanelCategory(objDel);
            }
            catch (Exception ex)
            {
                ret.RetValue = false;
                ret.Msg = "Dữ liệu đã được sử dung trong chương trình - không thể xóa !";
            }
            return ret;
        }


        public ReturnValue<bool> DelChanelCategory(ChanelCategory inChanelCategory)
        {
            ReturnValue<bool> ret = new ReturnValue<bool>(false, "");
            if (inChanelCategory == null)
            {
                ret.Msg = "input null";
                return ret;
            }
            try
            {
                repository.Delete(inChanelCategory);
                ret.RetValue = repository.SaveChanges() > 0;
            }
            catch (System.Exception ex)
            {
                ret.RetValue = false;
                ret.Msg = "Dữ liệu đã được sử dung trong chương trình - không thể xóa !";
            }
            return ret;
        }
    }
}
